<!-- 添加、修改药物 -->

<template>
	<view class="container">
		<lk-topbar :title="'药物'"  :backcolor="'#f5f5f5'" @back="back"></lk-topbar>
		<view class="p30">
			<view class="p20">
				<view class="recordItem">
					<view class="recordChild">
						<view class="recordName">药名</view>
					</view>
					<view class="recordChild"  >
						<view class="">{{med_name?med_name:'点击选择'}} </view>
						<view class="icon arrow"></view>
					</view>
				</view>
				
				<lk-lists v-show="showMed"  :list="medicines" :name='"medicines"' :act='medi' :isAdd='false'  @addData="addData" @select="select" ></lk-lists>
				
				
				<view class="recordItem">
					<view class="recordChild">
						<view class="recordName">规格</view>
					</view>
					<view class="recordChild" @click="showDetail(0)">
						<view class="">{{spec?spec:'点击选择'}}</view>
						<view class="icon arrow"></view>
					</view>
				</view>
				<lk-lists v-show="showArr[0]" :list="specificate" :name='"specificate"' :act='speci' @addData="addData" @select="select"  ></lk-lists>
				
				
				
				<view class="recordItem">
					<view class="recordChild">
						<view class="recordName">用量</view>
					</view>
					<view class="recordChild" @click="showDetail(1)">
						<view class="">{{dosage?dosage:'点击选择'}}</view>
						<view class="icon arrow"></view>
					</view>
				</view>
				<lk-lists v-show="showArr[1]" :list="dosages" :name='"dosages"' :act='dosagei'  @addData="addData" @select="select" ></lk-lists>
			</view>
		</view>
		
		<button @click="save" class="btn" type="default">保存</button>
			<lk-addipt v-show="showAdd" @addSubmit='addSubmit' @close="close" ></lk-addipt>
	</view>
</template>

<script>
	import lkLists from "../../components/lk-lists/index.vue"
	import lkAddipt from "../../components/lk-addipt/index.vue"
	import meds from '../../core/medicine.js'
	export default {
		name:"medicine",
		data() {
			return {
				showAdd:false,
				showMed:true,
				showArr:[false,false],
				// medicines:['美多芭','息宁','森福罗'],
				specificate:['125.0mg','250.0mg','375.0mg'],
				dosages:['1/4片','1/2片'],
				med_name:"",
				medi:-1,
				spec:'',
				speci:-1,
				dosage:'',
				dosagei:-1,
				pindex:0,
				cindex:0,
				isSave:true,
			}
		},
		props:{
			
		},
		computed:{
			medicines(){
				let medi = meds.medArr.indexOf(this.med_name);
				this.medi = medi != -1 ? medi : 0;
				(medi == -1) && (this.med_name = meds.medArr[0])  
				this.specificate = meds.allMed[this.medi].spc;
				this.speci = this.specificate.indexOf(this.spec);
				this.dosages =  meds.allMed[this.medi].amount;
				this.dosagei = this.dosages.indexOf(this.dosage)
				return meds.medArr
			}
		},
		components:{
			lkLists,
			lkAddipt
		},
		onLoad(o) {
			this.pindex = o.pi;
			this.cindex = o.ci;
			if(typeof o.data != "undefined"){
				o.data = JSON.parse(o.data);
				this.showMed = false;
				this.med_name=o.data.med_name;
				this.spec=o.data.spec;
				this.dosage=o.data.dosage;
			}
			let pages = getCurrentPages();
			let pre = pages[pages.length -2];
			delete(pre.data.med);			
		},
		onShow() {
			
		},
		methods: {
			// 保存药物数据
			save(){
				let pages = getCurrentPages();
				let pre = pages[pages.length -2];
				if(this.med_name == ''){
					uni.showToast({
						title:"请选择药名",
						icon:"error"
					})
					return false;
				}
				if(this.spec == ''){
					uni.showToast({
						title:"请选择规格",
						icon:"error"
					})
					return false;
				}
				if(this.dosage == ''){
					uni.showToast({
						title:"请选择用量",
						icon:"error"
					})
					return false;
				}
				pre.setData({
					med:{
						med_name:this.med_name,
						spec:this.spec,
						dosage:this.dosage,
					},
					pindex:this.pindex,
					cindex:this.cindex,
				})
				this.isSave = true;
				this.$routeJump.back()
			},
			/**
			 * @param {Object} o 返回的值和哪一项
			 */
			select(o){
				this.isSave =false;
				switch(o.name){	
					case 'medicines':
						this.med_name = o.content;
						break;
					case 'specificate':
						this.spec = o.content;
						this.speci = this.specificate.indexOf(this.spec);
						console.log(this.speci);
						break;
					case 'dosages':
						this.dosage = o.content;
						this.dosagei = this.dosages.indexOf(this.dosage)
						break;
					default:
						break;
				}
			},
			/** 确定添加
 			 * @param {Object} data 添加的内容
			 */
			addSubmit(data){
				this.isSave = false;
				switch(this.addName){
					case 'medicines':
						this.medicines.push(data);
						break;
					case 'specificate':
						this.specificate.push(data);
						break;
					case 'dosages':
						this.dosages.push(data);
						break;
					default:
						break;
				}
				this.showAdd = false;
			},
			/** 显示添加弹窗
			 * @param {Object} name 添加哪项
			 */
			addData(name){
				this.addName = name;
				this.showAdd  =true;
			},
			/**
			 * 显示详情列表
			 * @param {Object} index 哪一个
			 */
			showDetail(index){
				this.showArr.splice(index,1, !this.showArr[index])
			},
			// 关闭添加弹窗
			close(){
				this.showAdd = false;
			},
			// 页面返回
			back(){
				let _this = this;
				if(_this.isSave){
					_this.$routeJump.back()
				}else{
					wx.showModal({
						title:"提示",
						content:"你还没保存，是否保存？",
						success(res){
							if(res.confirm){
								_this.save()
							}else{
								_this.$routeJump.back()
							}
						}
					})
				}
			}
		}
	}
	
</script>

<style lang="scss" scoped>
	.container{
		background-color: #f5f5f5;
		.recordItem{
			padding: 20rpx 0;
			font-size: 36rpx;
			@include flex(space-between,center);
			.recordChild{
				@include flex(flex-start,center)
				.recordName{
					width:250rpx
				}
			}
		}
		.btn{
			width: 75%;
			height: 84rpx;
			line-height: 84rpx;
			margin: 0 auto 50rpx !important;
			border-radius: 50rpx;
			background-color: #ffb526;
			margin: 40rpx 0;
		}
	}	
</style>
